package com.xh.bussiness.tiku.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xh.bussiness.system.model.Dictionary;
import com.xh.bussiness.tiku.bean.request.SaveQuestionTypeRelationRequest;
import com.xh.bussiness.tiku.dao.QuestionTypeRelationMapper;
import com.xh.bussiness.tiku.model.QuestionTypeRelation;
import com.xh.bussiness.tiku.service.IQuestionTypeRelationService;
import com.xh.core.bean.XhPage;
import com.xh.core.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/*
 * @Author daiwei
 * @Description
 * @Date 2019/3/18 13:55
 **/
@Service
public class QuestionTypeRelationServiceImpl extends BaseServiceImpl<QuestionTypeRelationMapper, QuestionTypeRelation> implements IQuestionTypeRelationService {


    @Override
    public List<QuestionTypeRelation> queryBySubject(String subject) {
        QueryWrapper<QuestionTypeRelation> qw = new QueryWrapper<>();
        qw.eq("subject_id", subject);
        qw.orderByAsc("tool_question_type");
        return this.dao.selectList(qw);
    }

    @Override
    public QuestionTypeRelation queryByJyQuesTypeName(String subject,String jyQuestionTypeName) {
        QueryWrapper<QuestionTypeRelation> qw = new QueryWrapper<>();
        qw.eq("subject_id", subject);
        qw.eq("jy_question_type_name", jyQuestionTypeName);
        return this.dao.selectOne(qw);
    }

    @Override
    public QuestionTypeRelation queryByJyQuestionType(String subject, Integer jyQuestionType) {
        QueryWrapper<QuestionTypeRelation> qw = new QueryWrapper<>();
        qw.eq("subject_id", subject);
        qw.eq("jy_question_type", jyQuestionType);
        return this.dao.selectOne(qw);
    }

    @Override
    public Integer queryToolQuestionTypeByJyType(String subject, Integer jyQuestionType) {
        QueryWrapper<QuestionTypeRelation> qw = new QueryWrapper<>();
        qw.eq("subject_id", subject);
        qw.eq("jy_question_type", jyQuestionType);
        QuestionTypeRelation questionTypeRelation = this.dao.selectOne(qw);
        if (questionTypeRelation != null) {
            return questionTypeRelation.getToolQuestionType();
        }
        return null;
    }

    @Override
    public List<QuestionTypeRelation> queryBaticTypeBySubject(String subject) {
        QueryWrapper<QuestionTypeRelation> qw = new QueryWrapper<>();
        qw.eq("subject_id", subject);
        //基础题类型 小于5
        qw.le("tool_question_type",5);
        return this.dao.selectList(qw);
    }

    @Override
    @Transactional
    public void reflash() {
        List<String> quests = new ArrayList<>();
        quests.add("选择题");
        quests.add("多选题");
        quests.add("填空题");
        quests.add("判断题");
        quests.add("解答题");
        quests.add("排序题");
        quests.add("连线题");
        quests.add("匹配题");
        quests.add("阅读理解");
        quests.add("完形填空");
        quests.add("综合题");
        quests.add("其他");

        List<String> dataIN = new ArrayList<>();
        dataIN.add("选择题");
        dataIN.add("多选题");
        dataIN.add("填空题");
        dataIN.add("判断题");
        dataIN.add("解答题");
//        dataIN.add("排序题");
//        dataIN.add("连线题");
//        dataIN.add("匹配题");
        dataIN.add("阅读理解");
        dataIN.add("完形填空");
//        dataIN.add("综合题");
//        dataIN.add("其他");

        for (int i = 0;i <quests.size();i++) {
            String quest = quests.get(i);
            if(!dataIN.contains(quest)){
                QueryWrapper<QuestionTypeRelation> qw = new QueryWrapper<>();
                qw.notIn("jy_question_type_name", dataIN);
                List<QuestionTypeRelation> questionTypeRelations = this.dao.selectList(qw);
                for (QuestionTypeRelation questionTypeRelation : questionTypeRelations) {
                    questionTypeRelation.setToolQuestionType(12);
                    questionTypeRelation.setToolQuestionTypeName("其他");
                }
                this.batchUpdate(questionTypeRelations);
            }

        }

    }

    @Override
    public void batchSaveQuestionTypeRelation(SaveQuestionTypeRelationRequest request) {
        if (CollectionUtils.isEmpty(request.getSubjects())) {
            return;
        }
        for (Dictionary subject : request.getSubjects()) {
            QueryWrapper<QuestionTypeRelation> qw = new QueryWrapper<>();
            qw.eq("subject_id", subject.getCode());
            qw.eq("jy_question_type_name", request.getQuestionTypeName());
            Integer count = this.selectCount(qw);
            if (count > 0) {
                continue;
            }
            Integer jyType = queryMaxJyTypeValBySubject(subject.getCode());

            QuestionTypeRelation relation = new QuestionTypeRelation();
            relation.setSubjectId(subject.getCode());
            relation.setSubjectName(subject.getName());
            relation.setToolQuestionType(request.getBasicQuestionType().getId());
            relation.setToolQuestionTypeName(request.getBasicQuestionType().getName());
            relation.setJyQuestionType((jyType + 1));
            relation.setJyQuestionTypeName(request.getQuestionTypeName());

            this.insert(relation);
        }
    }

    @Override
    public XhPage<QuestionTypeRelation> queryPage(XhPage page, String subjectCode, String jyQuestionTypeName) {
        QueryWrapper<QuestionTypeRelation> qw = new QueryWrapper<>();
        if (!StringUtils.isEmpty(subjectCode)) {
            qw.eq("subject_id", subjectCode);
        }
        if (!StringUtils.isEmpty(jyQuestionTypeName)) {
            qw.like("jy_question_type_name", jyQuestionTypeName);
        }
        qw.orderByAsc("jy_question_type");

        return (XhPage) this.dao.selectPage(page,qw);
    }

    @Override
    public void batchChangeStatus(String[] ids, Integer status) {
        List<QuestionTypeRelation> dictionaries = super.getDao().selectBatchIds(Arrays.asList(ids));
        if (!CollectionUtils.isEmpty(dictionaries)) {
            for (QuestionTypeRelation typeRelation : dictionaries) {
                typeRelation.setStatus(status);
            }
            super.batchUpdate(dictionaries);
        }
    }

    @Override
    public List<Map<String, Object>> queryRelationMapBySubject(String subjectId) {
        return this.dao.queryRelationMapBySubject(subjectId);
    }

    public Integer queryMaxJyTypeValBySubject(String subjectCode) {
        QueryWrapper<QuestionTypeRelation> qw = new QueryWrapper<>();
        qw.eq("subject_id", subjectCode);
        qw.orderByDesc("jy_question_type");
        QuestionTypeRelation relation = this.selectFirst(qw);
        if (relation != null) {
            return relation.getJyQuestionType();
        }
        return 1;
    }
}
